I have noticed that in a case tempStringOf( buffer ) does not return the same string as stringOf( buffer ). It appears that the stringOf() function respects the length field in a buffer structure where as the tempStringOf() function does not. A buffer object is a pointer to a structure with several "fields". A few of them have obvious meaning. The decoded fields I found are as follows: offset 0 length 4 Bytes allocated to the buffer string. offset 8 length 4 Pointer to the memory region containing the string. offset 12 length 4 The valid length of the string in buffer. I was using the following code to take the last three characters off the end of my buffer's string: length( b1, length( b1 ) - 3 ) My code was working when I used stringOf(b1) after the shortening. I then switched to using tempStringOf(b1) instead and then found the last three characters were still a part of the string. The buffer length() function shortened the buffer by changing the buffer's length in the buffer structure but did not change the location of the string's null terminator in the string. The tempStringOf() function appears to just return a pointer to the buffer's string. Where as the stringOf() function is making a copy of the buffer's string and littering the string table with it. So I suppose the tempStringOf() function is doing what is desired. However the buffer length() function is being "lazy" and not providing a result that works with the tempStringOf() function. Below is some code to demonstrate this: {code}
void printHexNibble( int nibble ) {/code} GothSloth - Tue Aug 13 13:35:38 EDT 2013 |
Re: Note on tempStringOf vs stringOf Yes I've noticed that as well. There was another problem when the Buffer naturally had a null character in it (such as when reading an esecutable file into the Buffer) but I don't recall exactly the issue. Anyway, don't use "tempStringOf" when you have adjusted it's length. |
Re: Note on tempStringOf vs stringOf Just set the null terminator as well as the length:
-Adam |
Re: Note on tempStringOf vs stringOf Adamarla - Wed Aug 14 03:26:24 EDT 2013 Just set the null terminator as well as the length:
-Adam Thanks Adam excellent solution. I had tried using a sub-string extraction only to discover it creates and returns a new buffer instead of writing into my buffer. Ugg ... that's when I decided it was time to go sleep on it ;)
void func( Buffer input, Buffer output ) |
Re: Note on tempStringOf vs stringOf GothSloth - Wed Aug 14 08:48:58 EDT 2013 Thanks Adam excellent solution. I had tried using a sub-string extraction only to discover it creates and returns a new buffer instead of writing into my buffer. Ugg ... that's when I decided it was time to go sleep on it ;)
void func( Buffer input, Buffer output ) Yes, by extracting part of a Buffer you create a new Buffer with the extracted characters. This is a common way to leak Buffer objects. You always need to assign the result to a Buffer variable so it can be deleted.
-Adam |
Re: Note on tempStringOf vs stringOf GothSloth - Wed Aug 14 08:48:58 EDT 2013 Thanks Adam excellent solution. I had tried using a sub-string extraction only to discover it creates and returns a new buffer instead of writing into my buffer. Ugg ... that's when I decided it was time to go sleep on it ;)
void func( Buffer input, Buffer output ) First off, if you figure to allocate or de-allocate a Buffer in a call parameter you'd want to call-by reference: Modifying contents doesn't require that.
You could cast the buffer substring to make sure you get a string
You can use combine
-Louie |
Re: Note on tempStringOf vs stringOf Adamarla - Wed Aug 14 09:12:08 EDT 2013 Yes, by extracting part of a Buffer you create a new Buffer with the extracted characters. This is a common way to leak Buffer objects. You always need to assign the result to a Buffer variable so it can be deleted.
-Adam Does this behaviour also apply to this code:
char chrTest = aBuffer[1] //Does this line also cause a memory leak?
if(chrTest == '1'){
print("cool")
}
I would guess not because I assigned it to a char type variable. But this is DXL so you can never know. Thank you in advance for your help :)
Edit: Fixed typo |
Re: Note on tempStringOf vs stringOf LucLeto - Fri Aug 21 04:58:33 EDT 2015 Does this behaviour also apply to this code:
char chrTest = aBuffer[1] //Does this line also cause a memory leak?
if(chrTest == '1'){
print("cool")
}
I would guess not because I assigned it to a char type variable. But this is DXL so you can never know. Thank you in advance for your help :)
Edit: Fixed typo Chars don't leak memory, they exist only on the stack. string sTest = aBuffer[1:1] will also not leak! Since no temp Buffer will be created, the string ::[] ( Buffer, Range_) operator is called. string sTest = aBuffer[1:1] "" will leak. Temp Buffer is created, since Buffer ::[] (Buffer, Range_) operator is called and concatenated to a string. Regards, Mathias |
Re: Note on tempStringOf vs stringOf Mathias Mamsch - Fri Aug 21 05:10:54 EDT 2015 Chars don't leak memory, they exist only on the stack. string sTest = aBuffer[1:1] will also not leak! Since no temp Buffer will be created, the string ::[] ( Buffer, Range_) operator is called. string sTest = aBuffer[1:1] "" will leak. Temp Buffer is created, since Buffer ::[] (Buffer, Range_) operator is called and concatenated to a string. Regards, Mathias Wow that was quick. Thank you :) But what happens if I do this:
if(aBuffer[1] == '1'){
//then do something
}
This won't leak either, because I derive of your posts that only string range expressions of buffers have this strange behaviour.
Offtopic: To Mathias, Louie and all the other DXL gents. Have you ever considered putting all that off manuel knowlegde in a book? I would definitely buy that. I struggle every day at getting the best out of our scripts. Even 60% of the time I reimplement old libraries to adept to current needs. |